/**
 * @file LocalConsoleUI.as
 * @link poplaryy@gmail.com
 * @author dengyang
 * @playerversion flash player 9+
 * @asversion 3.0
 * @version 0.6
 * @builddate  2009-12-30
 * @updatedate 2009-12-30
 */   
package org.baicaix.demo.localconnect {
	import org.baicaix.events.DataEvent;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.ui.Keyboard;

	/**
	 * @author dengyang
	 */
	public class LocalConsoleUI extends Sprite {
		//显示信息组件
		private var viewInfo : TextField;
		//输入框
		private var commandTxt : TextField;
		//清除按钮
//		private var clearBtn : SimpleButton;
//		private var sendBtn : SimpleButton;
//		
		private var info : LocalConsoleClient;
		private var infoViewNum : int;

		public function LocalConsoleUI() {
			
			info = new LocalConsoleClient();
			info.addEventListener(LocalConsoleClient.EVENT_PRINT, onPrint);
			
			initUI();
		}
		
		/**
		 * 初始化UI
		 */
		private function initUI() : void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			viewInfo = new TextField;
			commandTxt = new TextField;
//			clearBtn = new SimpleButton();
//			sendBtn = new SimpleButton();
			
			viewInfo.background = true;
			viewInfo.backgroundColor = 0x000000;
			viewInfo.textColor = 0x00ff00;
			viewInfo.wordWrap = true;
			viewInfo.mouseWheelEnabled = true;
			
			commandTxt.background = true;
			commandTxt.backgroundColor = 0x000000;
			commandTxt.textColor = 0x00ff00;
			commandTxt.type = TextFieldType.INPUT;
			
//			var viewFormat : TextFormat = new TextFormat();
//			viewFormat.size = 12;
			
//			viewInfo.setStyle('textFormat', viewFormat);
//			commandTxt.setStyle('textFormat', viewFormat);
			commandTxt.addEventListener(KeyboardEvent.KEY_DOWN, onSend);
//			clearBtn.addEventListener(MouseEvent.CLICK, onClear);
//			sendBtn.addEventListener(MouseEvent.CLICK, onSend);

			stage.addEventListener(Event.RESIZE, adjustSreen);
			adjustSreen();
		}

		
		/**
		 * 调整屏幕组件大小和位置
		 */
		private function adjustSreen(event : Event = null) : void {
			this.addChild(viewInfo);
			this.addChild(commandTxt);
//			this.addChild(clearBtn);
//			this.addChild(sendBtn);
			viewInfo.width = stage.stageWidth;
			viewInfo.height = stage.stageHeight - 30;
			viewInfo.x = 0;
			viewInfo.y = 0;
			commandTxt.width = stage.stageWidth;// - 80 - 60;
			commandTxt.x = 0;
			commandTxt.y = stage.stageHeight - 25;
//			clearBtn.x = stage.stageWidth - 60;
//			clearBtn.y = stage.stageHeight - 25;
//			sendBtn.x = stage.stageWidth - 100;
//			sendBtn.y = stage.stageHeight - 25;
		}

		private function onClear(event : Event) : void {
			viewInfo.text = '';
			infoViewNum = 0;
		}
		
		private function onSend(event : KeyboardEvent) : void {
			if(event.keyCode == Keyboard.ENTER) {
				
				if(commandTxt.text == "") return;
				
				if(commandTxt.text == "clear") {
					this.onClear(event);
				} else {
					info.send(commandTxt.text);
					//only for test
//					info.print(commandTxt.text);
				}
				commandTxt.text = "";
			}
		}

		public function onPrint(event : DataEvent) : void {
			infoViewNum++;
			viewInfo.appendText(infoViewNum + ': ' + event.data + '\n');
			viewInfo.scrollV = viewInfo.maxScrollV;
		}
	}
}
